package com.alien.admin.context;

import com.alien.admin.context.task.DeployTaskManager;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private DeployTaskManager deployTaskManager;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        log.info("Spring容器启动成功");
        log.info("开始监听redis消息队列");
        consumeQueue();
    }

    @Async
    public void consumeQueue() {
        try {
            while (true) {
                try {
                    deployTaskManager.consumeTask();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            // ignore
        }
    }
}
